<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/30
 * Time: 14:26
 */

namespace app\alliance\controller;


use Protocol\Curl;
use Redis\Redis;
use think\Db;

class Demo
{
    public $_Redis;

    function __construct()
    {
        $this->_Redis = new Redis();
    }

    function demo()
    {
//        $this->_Redis->delete('recommendNum');
 //       $this->_Redis->flush();
   //     var_export(1);


    }
    function demos()
    {
        $redis=new Redis();
        $code=input("get.code");
        $grid=input("get.grid");
        $level=input("get.level");
        $userMatch=$redis->hGetJson("member_business",$code);
        //$redis->hDel("member_business",$code);
        dump($userMatch);
    }
    function demoed()
    {
        $code=input("get.code");
        $team_id=input("get.team_id");
        $level=input("get.level");
        $min=input("get.min");
        $max=input("get.max");
        $grid=input("get.grid");
        $list=$this->_Redis->hGetJson("userMatchBussiness",$code."and".$grid);
        //$this->_Redis->hDel("userMatchBussiness",$code."and".$grid);
        //$this->_Redis->hDel("matchListen",$code."and".$grid);
        $a=$this->_Redis->hGetJson("matchListen",$code."and".$grid);
        dump($list);
        dump($a);
        //$this->_Redis->hDel("member_sale_bussiness",$level);
        $b=$this->_Redis->hGetJson("member_sale_bussiness",$level);
        dump($b);
        //$business_id=input("get.business_id");
        //$c=$this->_Redis->hGetJson("trade_protect",strval($business_id));
        //$this->_Redis->hDel("team" . $team_id . "level" . strval($level), $min . "~" . $max);
        //dump($c);
        //dump(123);
    }

    function demoss()
    {
        ini_set('default_socket_timeout', -1);
        ignore_user_abort(true);     // 忽略客户端断开
        set_time_limit(0);           // 设置执行不超时
        $this->_Redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
        $this->_Redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallback');
    }

    function getcode(){
        $phone = input('get.phone');
        $redisCode = $this->_Redis->hGet('smsCode', strval($phone));
        dump($redisCode);
    }

    function addIntegral(){
        $memberCode = input('get.code');
        $phone = input('get.phone');
        $logConsumption = (new Curl())->post(config('leegame.leeBridge_api') . config('leegame.member_consumption'), array('phone' => $phone));
       dump($logConsumption);
        $logConsumption = json_decode($logConsumption, true);
        dump($logConsumption);
        addIntegral_branch($logConsumption, $memberCode);
    }

    function demoMatch()
    {
        $code=input("get.code");
        $grid=input("get.grid");
        $level=input("get.level");

//        $a=$this->_Redis->hGetJson("member_business",$code);
//        dump($a);
//        $b=$this->_Redis->hGetJson("matchnum",$code);
//        dump($b);
        $mb=$this->_Redis->hGetJson("userMatchBussiness",$code."and".$grid);
//        unset($mb[0]);
//        var_dump($mb);$mb=$this->_Redis->hGetJson("userMatchBussiness",$code."and".$grid);
////        unset($mb[0]);
var_dump($mb);
      //  $mb=$this->_Redis->hSetJson("userMatchBussiness",$code."and".$grid,array_values($mb));

//        $c=$this->_Redis->hGetJson("member_sale_bussiness",$level);
//        $c[0]['team_id']=1;
//        $this->_Redis->hSetJson("member_sale_bussiness",$level,$c);
//        dump($c);
        $jobService=new \app\service\jobService();
//        $jobService->timedTaskExec();
//        $jobService->protectExec();
        $jobService->listenTaskExecc();

    }

    function demoClearn()
    {
        $code=input("get.code");
        $grid=input("get.grid");
        $this->_Redis->hDel("member_business",$code);
        $mb=$this->_Redis->hDel("userMatchBussiness",$code."and".$grid);
        var_dump(123);
    }

    function demoLang(){
        ignore_user_abort(true);     // 忽略客户端断开
        set_time_limit(0);           // 设置执行不超时
        $num=0;
        while(true){
            echo("匹配中");
            $fp = fopen('test.txt','a+');
            if($num==100){
                return true;
                break;
            }
            $num++;
            sleep(5);
            $str = ob_get_contents();
            // 若实际输出内容长度小于该值将可能导致主动断开失败
            header('Content-Length: '. strlen($str));
            ob_start();
            ob_flush();
            flush();
            fwrite($fp,date("Y-m-d H:i:s"). "去匹配第{$num}次\n");
            fclose($fp);
        }
    }

    function demoma(){
        $list= Db::table("member_business mb")
            ->field("mb.*,m.name,m.phone")
            ->join("member m","m.code=mb.member_code")
            ->where(array("mb.status"=>3))->select();
        if(!empty($list)) {
            $_redis = new Redis();
            foreach ($list as $k => $v) {
                # 查询是否在用户栏位中
                $grid = Db::table("member_business_grid")->field("grid")->where(array("member_code" => $v['member_code'], "member_business_id" => $v['id']))->find();
                if (!empty($grid['grid'])) {
                    $v["sell_grid"] = $grid['grid'];
                }
                # 获取用户信息
                $v['sale_member_name'] = $v['name'];
                $v['sale_member_phone'] = $v['phone'];
                unset($v['name'], $v['phone']);
                # 计算当前出售价格处于哪种收益阶段
                $detail = getsIndustryDetail($v['industry_id']);
                foreach ($detail['cycleIncome'] as $ck => $cv) {
                    if ($cv['min_num'] <= $v['sale_price'] and $v['sale_price'] < $cv['max_num']) {
                        $v['cycle_period'] = $cv['cycle_period'];
                        $v['now_day_income'] = $cv['day_income'];
                        break;
                    }
                }
                # 获取列表中是否包含这个生意
                $_sale_list = $_redis->hGetJson("member_sale_bussiness", $v['earnings_level']);
                if(!empty($_sale_list)){
                    $_id_coll = array_column($_sale_list, 'id');
                    if (!in_array($v['id'], $_id_coll)) {
                        array_push($_sale_list,$v);
                    }
                }else{
                    $_sale_list=array();
                    array_push($_sale_list,$v);
                }
                $_redis->hSetJson("member_sale_bussiness", $v['earnings_level'],$_sale_list);
            }
            var_dump("成功了");
        }
    }

    function demoCleanRepet()
    {
        $level=input("get.level");
        $repet_id=input("get.repet_id");
        $_redis=new Redis();
        $_sale_list = $_redis->hGetJson("member_sale_bussiness", $level);
        if(!empty($_sale_list)){
            foreach($_sale_list as $k=>$v){
                if($v['id']==$repet_id){
                    unset($_sale_list[$k]);
                }
            }
            $_redis->hSetJson("member_sale_bussiness", $level,array_values($_sale_list));
            var_dump("删除了");
        }
    }

    static function filterArray($arr)
    {
        $_value_section=[[200,900],[900,1500],[1500,2200],[2200,3000]];
        $team_id = null;
        $not_precedence=array();
            # 判断是否为投放行业
            if(!empty($arr['lanunch']) and $arr['lanunch']==2){
                # 判断是否有剩余栏位限制
                if($arr['plurs_grid']>0 and $arr['plurs_grid']>self::$_plurs_grid){
                    return false;
                }
                # 判断是否有优先级限制
                if($arr['precedence']==2){
                    # 放入后匹配数组
                    if(empty($not_precedence)){
                        $not_precedence=array();
                        array_push($not_precedence,$arr);
                    }else{
                        # 不在后匹配数组中 放入
                        if(!in_array($arr['id'],array_values(array_column($not_precedence,'id')))){
                            array_push($not_precedence,$arr);
                        }
                    }
                    return false;
                }

            }
            if(!empty($arr['team_id'])){
                $_team_collon=explode(',',$arr['team_id']);
                if(!empty($_team_id) and !in_array($_team_id,$_team_collon)){
                    return false;
                }
            }
            $_continue=false;
            foreach($_value_section as $k=>$v){
                if($arr['sale_price']>=$v[0] and $arr['sale_price']<$v[1]){
                    $_continue=true;
                    break;
                }
            }
            if(!$_continue){
                return false;
            }
            return true;

    }


}